<?php

class AliPayment {

    function get_payform($order_info) {
        $order_info = $this->get_order_info($order_info);
        
        import('Mall.Payment.Alipay.AopClient', '', '.php');
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = C('app_alipay_partner_id');
        $aop->rsaPrivateKey = C('app_alipay_private_key');
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = C('app_alipay_public_key');
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay

        import('Mall.Payment.Alipay.request.AlipayTradeAppPayRequest', '', '.php');
        $request = new \AlipayTradeAppPayRequest();
        $bizcontent = "{\"body\":\"{$order_info['order_sn']}\","
                . "\"subject\":\"{$order_info['order_sn']}\","
                . "\"out_trade_no\":\"{$order_info['order_sn']}\","
                . "\"total_amount\":\"{$order_info['money']}\","
                . "\"product_code\":\"QUICK_MSECURITY_PAY\""
                . "}";
        $request->setNotifyUrl(C('SITE_URL') . U('Paynotify/alipay'));
//         $request->setNotifyUrl('http://www.jingjing.shop' . U('Paynotify/alipay'));

        $request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);

//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
//        echo htmlspecialchars($response); //就是orderString 可以直接给客户端请求，无需再做处理。

        echo json_encode(array(
            'code' => '10000',
            'message' => '',
            'result' => array('response' => $response),
        ));
    }

    function verify_notify() {
        import('Mall.Payment.Alipay.AopClient', '', '.php');
        $aop = new \AopClient;
        
        $aop->alipayrsaPublicKey = C('app_alipay_public_key');
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        if ($flag) {
            $notify_result = array(
                'out_trade_no' => $_POST["out_trade_no"], #商户订单号
                'trade_no' => $_POST['trade_no'], #交易凭据单号
                'total_fee' => $_POST["total_amount"], #涉及金额
                'trade_status' => '1',
            );
        } else {
            $notify_result = array(
                'trade_status' => '0',
            );
        }
        return $notify_result;
    }

    function get_order_info($order_info) {
        if ($order_info['is_order_array']) {
            $order_info0 = $order_info;
            $order_info = array();
            foreach ($order_info0['item'] as $fe_order_info) {
                if (!$order_info['money']) {
                    $order_info['money'] = bcsub($fe_order_info['order_amount'], $fe_order_info['pd_amount'], 2);

                } else {

                    $order_info['money'] = bcadd($order_info['money'], bcsub($fe_order_info['order_amount'], $fe_order_info['pd_amount'], 2), 2);
//                    lichen 2019/5/25 end

                }

                if (!($new_order_sn)) {
                    $new_order_sn = D('Order')->_gen_order_sn();
                    $out_trade_no = $new_order_sn;
                    $orders_group = $fe_order_info['order_id'];
                    $order_info['buyer_id'] = $fe_order_info['buyer_id'];
                    $order_info['payment_name'] = $fe_order_info['payment_name'];
                    $order_info['buyer_name'] = $fe_order_info['buyer_name'];
                    $order_info['order_sn'] = $new_order_sn;
                    $order_info['order_id'] = $fe_order_info['order_id'];
                } else {
                    $orders_group.=',' . $fe_order_info['order_id'];
                }
            }
            $account_log_mod = M('account_log');
            $account_log = array(
                'user_id' => $order_info['buyer_id'],
                'user_name' => $order_info['buyer_name'],
                'order_sn' => $new_order_sn,
                'orders_group' => $orders_group,
                'add_time' => time(),
                'type' => ACCOUNT_TYPE_CZ,
                'money_flow' => 'income',
                'money' => $order_info['money'],
                'log_text' => $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(" . $order_info['payment_name'] . ")",
            );
            $account_log_mod->add($account_log);
        } else {
            $order_info['money'] = bcsub($order_info['order_amount'], $order_info['pd_amount'], 2);
            $order_info = $this->add_account_log($order_info);
            if (!$order_info) {
                return false;
            }
        }
        return $order_info;
    }

    function add_account_log($order_info) {
        $account_log_mod = M('account_log');
        $account_log = $account_log_mod->where('order_sn=' . $order_info['order_sn'])->find();
        if (empty($account_log)) {
            $account_log = array(
                'user_id' => $order_info['buyer_id'],
                'order_id' => $order_info['order_id'],
                'user_name' => $order_info['buyer_name'],
                'order_sn' => $order_info['order_sn'],
                'add_time' => time(),
                'type' => ACCOUNT_TYPE_CZ,
                'money_flow' => 'income',
                'money' => $order_info['money'],
                'log_text' => $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(" . $order_info['payment_name'] . ")",
            );
            $account_log_mod->add($account_log);
        } else {
            if ($account_log['complete'] == 1) {
                return false;
            }
            //如果更换支付方式，记录需发生变化。
            $data['log_text'] = $order_info['buyer_name'] . "充值" . $order_info['money'] . "元(" . $order_info['payment_name'] . ")";
            if ($account_log['money'] != $order_info['money']) {
                //订单金额发生改变会导致不允许支付，需要更换订单号
                $new_order_sn = D('Order')->_gen_order_sn();
                //金额不一致需修改。
                $data['money'] = $order_info['money'];
                $data['order_sn'] = $new_order_sn;
                //修改订单order_sn
                $order_mod = & m('order');
                $order_mod->where('order_id=' . $order_info['order_id'])->save(array('order_sn' => $new_order_sn));
                $order_info['order_sn'] = $new_order_sn;
            }
            $account_log_mod->where('id=' . $account_log['id'])->save($data);
        }
        return $order_info;
    }

}

?>
